package main

import (
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/hertz-contrib/cors"
	"github.com/tebeka/selenium"
	"log"
	"net/http"
	_ "net/http/pprof"
	"os"
	"path/filepath"
	"view-crawler-backend/internal/handler"
)

var h *server.Hertz

func main() {
	go func() {
		http.ListenAndServe("127.0.0.1:6060", nil)
	}()

	// 启动 WebDriver 服务
	dir, err := os.Getwd()
	if err != nil {
		log.Fatalf("浏览器驱动路径读取失败: %v\n", err)
	}
	_, err = selenium.NewChromeDriverService(filepath.Join(dir, "chromedriver.exe"), 4444)
	if err != nil {
		log.Fatalf("Selenium WebDirver启动失败: %v\n", err)
	}

	// 启动 hertz web 服务
	h = server.Default(
		server.WithHostPorts("127.0.0.1:8080"),
	)
	// https://github.com/cloudwego/hertz/issues/121
	h.NoHijackConnPool = true
	// 配置中间件
	h.Use(cors.New(cors.Config{
		AllowAllOrigins: true,
		AllowMethods:    []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:    []string{"Origin", "Content-Length", "Content-Type"},
	}))

	registerRouters()

	h.Spin()
}

func registerRouters() {
	h.POST("/task/create", handler.CreateTask)
	h.DELETE("/task/delete", handler.DeleteTask)
	h.PUT("/task/update", handler.UpdateTask)
	h.PUT("/task/updateTaskDefine", handler.UpdateTaskDefine)
	h.GET("/task/queryList", handler.QueryTaskList)
	h.GET("/task/queryDetail", handler.QueryTaskDetail)
	h.GET("/task/exec", handler.TaskExec)

	h.GET("category/queryAll", handler.QueryAllCategories)
	h.PUT("category/update", handler.UpdateCategory)
}
